#include <string>
#include <unordered_set>
#include <iostream>

using namespace std;

class Solution 
{
public:
    int lengthOfLongestSubstring(string s) 
    {
        if(s.length() == 0)
        {
            return 0;
        }
        
        unordered_set<char> store;
        store.insert(s[0]);
        int left = 0;
        int maxCount = 1;
        for(int right = 1; right < s.length(); right++)
        {
            auto it = store.find(s[right]);
            if(it != store.end())
            {
                while(s[left] != *it)
                {
                    store.erase(s[left]);
                    left++;
                }
                left++;
            }
            else
            {
                store.insert(s[right]);
            }
            int count = store.size();
            maxCount = max(maxCount, count);
        }
        return maxCount;
    }
};

int main() 
{
    Solution solution;
    
    // 测试案例1: "abcabcbb"
    cout << "测试案例1:" << endl;
    string s1 = "abcabcbb";
    cout << "输入: \"" << s1 << "\"" << endl;
    int result1 = solution.lengthOfLongestSubstring(s1);
    cout << "输出: " << result1 << endl;
    cout << "期望: 3 (子串 \"abc\")" << endl << endl;
    
    // 测试案例2: "bbbbb"
    cout << "测试案例2:" << endl;
    string s2 = "bbbbb";
    cout << "输入: \"" << s2 << "\"" << endl;
    int result2 = solution.lengthOfLongestSubstring(s2);
    cout << "输出: " << result2 << endl;
    cout << "期望: 1 (子串 \"b\")" << endl << endl;
    
    // 测试案例3: "pwwkew"
    cout << "测试案例3:" << endl;
    string s3 = "pwwkew";
    cout << "输入: \"" << s3 << "\"" << endl;
    int result3 = solution.lengthOfLongestSubstring(s3);
    cout << "输出: " << result3 << endl;
    cout << "期望: 3 (子串 \"wke\")" << endl << endl;
    
    // 测试案例4: 空字符串
    cout << "测试案例4 (空字符串):" << endl;
    string s4 = "";
    cout << "输入: \"" << s4 << "\"" << endl;
    int result4 = solution.lengthOfLongestSubstring(s4);
    cout << "输出: " << result4 << endl;
    cout << "期望: 0" << endl << endl;
    
    // 测试案例5: 单个字符
    cout << "测试案例5 (单个字符):" << endl;
    string s5 = "a";
    cout << "输入: \"" << s5 << "\"" << endl;
    int result5 = solution.lengthOfLongestSubstring(s5);
    cout << "输出: " << result5 << endl;
    cout << "期望: 1" << endl << endl;
    
    // 测试案例6: 无重复字符
    cout << "测试案例6 (无重复字符):" << endl;
    string s6 = "abcdef";
    cout << "输入: \"" << s6 << "\"" << endl;
    int result6 = solution.lengthOfLongestSubstring(s6);
    cout << "输出: " << result6 << endl;
    cout << "期望: 6" << endl << endl;
    
    // 测试案例7: 两个字符
    cout << "测试案例7 (两个字符):" << endl;
    string s7 = "au";
    cout << "输入: \"" << s7 << "\"" << endl;
    int result7 = solution.lengthOfLongestSubstring(s7);
    cout << "输出: " << result7 << endl;
    cout << "期望: 2" << endl << endl;
    
    // 测试案例8: 复杂情况
    cout << "测试案例8 (复杂情况):" << endl;
    string s8 = "dvdf";
    cout << "输入: \"" << s8 << "\"" << endl;
    int result8 = solution.lengthOfLongestSubstring(s8);
    cout << "输出: " << result8 << endl;
    cout << "期望: 3 (子串 \"vdf\")" << endl << endl;
    
    // 测试案例9: 数字和字符
    cout << "测试案例9 (数字和字符):" << endl;
    string s9 = "abba";
    cout << "输入: \"" << s9 << "\"" << endl;
    int result9 = solution.lengthOfLongestSubstring(s9);
    cout << "输出: " << result9 << endl;
    cout << "期望: 2 (子串 \"ab\" 或 \"ba\")" << endl << endl;
    
    // 测试案例10: 特殊字符
    cout << "测试案例10 (特殊字符):" << endl;
    string s10 = " !@#$%^&*()";
    cout << "输入: \"" << s10 << "\"" << endl;
    int result10 = solution.lengthOfLongestSubstring(s10);
    cout << "输出: " << result10 << endl;
    cout << "期望: 11 (整个字符串)" << endl << endl;
    
    // 测试案例11: 长重复模式
    cout << "测试案例11 (长重复模式):" << endl;
    string s11 = "abcdefabcdef";
    cout << "输入: \"" << s11 << "\"" << endl;
    int result11 = solution.lengthOfLongestSubstring(s11);
    cout << "输出: " << result11 << endl;
    cout << "期望: 6 (子串 \"abcdef\")" << endl << endl;
    
    return 0;
}